package com.md.application.service;

import com.md.api.ModuleApi;
import com.md.application.assembler.ModuleAssembler;
import com.md.domain.model.po.ModulePO;
import com.md.domain.service.AppDomainService;
import com.md.model.page.PageResponse;
import com.md.model.system.CommonResult;
import com.md.request.ModuleRequest;
import com.md.request.QueryModuleRequest;
import com.md.response.ModuleResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static com.md.application.assembler.ModuleAssembler.buildModulePO;
import static com.md.application.assembler.ModuleAssembler.buildModulePO2Response;

/**
 * @author 刘骄阳
 * 2023-08-08 14:15
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ModuleApplicationService implements ModuleApi {

  private final AppDomainService appDomainService;


//  public CommonResult<PhoneCodeResponse> sendCode(final PhoneCodeRequest request) {
//    String cache = redisUtils.getHash(RedisConstants.PHONE_CODE, request.getPhone());
//    if (!ObjectUtils.isEmpty(cache)) {
//      PhoneCodeResponse cacheResponse = StringUtil.jsonToObject(cache, PhoneCodeResponse.class);
//      LocalDateTime sendTime = cacheResponse.getSendTime();
//      Dictionary effectiveTime = dictionaryRepository.queryByKey(DictionaryEnum.CODE_EFFECTIVE_TIME.getMessage());
//      if (DateUtils.judgeDateIsEffectiveTime(sendTime, Long.parseLong(effectiveTime.getValue()))) {
//        throw ServerException.build(ErrorEnum.CODE_HAS_EXISTS);
//      }
//    }
//    PhoneCodeResponse response = new PhoneCodeResponse();
//    try {
//      Dictionary length = dictionaryRepository.queryByKey(DictionaryEnum.CODE_LENGTH.getMessage());
//      String smsCode = SystemUtils.createSmsCode(Integer.parseInt(length.getValue()));
//      SendSmsResponse smsResponse = aliYunUtils.sendSmsCode(request.getPhone(), smsCode);
//      String code = smsResponse.getBody().getCode();
//      SmsEnum of = SmsEnum.of(code);
//      if (ObjectUtils.isEmpty(of) || !Objects.equals(of.getCode(), SmsEnum.OK.getCode())) {
//        throw ServerException.build(ErrorEnum.CODE_SEND_ERROR);
//      }
//      response.setCode(code);
//      response.setSendTime(LocalDateTime.now());
//      redisUtils.setHash(RedisConstants.PHONE_CODE, request.getPhone(), response.toString());
//    } catch (Exception e) {
//      log.error("短信发送失败: {}", e.getMessage(), e);
//      return CommonResult.error(ErrorEnum.CODE_SEND_ERROR);
//    }
//    return CommonResult.success(response);
//  }

  @Override
  public CommonResult<ModuleResponse> create(final ModuleRequest request) {
    ModulePO modulePO = buildModulePO(request);
    appDomainService.checkModuleExists(modulePO.getModelName(), modulePO.getTenantId(),
      modulePO.getBrandId(), modulePO.getUserId());
    appDomainService.createModule(modulePO);
    return CommonResult.success(buildModulePO2Response(modulePO));
  }

  @Override
  public CommonResult<ModuleResponse> update(final ModuleRequest request) {
    ModulePO modulePO = buildModulePO(request);
    appDomainService.update(modulePO);
    return CommonResult.success(buildModulePO2Response(modulePO));
  }

  @Override
  public CommonResult<ModuleResponse> query(final QueryModuleRequest request) {
    return CommonResult.success(appDomainService.queryModuleDetail(ModuleAssembler.buildModulePO(request)));
  }

  @Override
  public CommonResult<PageResponse<ModuleResponse>> queryList(final QueryModuleRequest request) {
    return CommonResult.success(appDomainService.queryModuleByPage(ModuleAssembler.buildQueryModuleBO(request)));
  }
}
